package com.duduwolf.handgis.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.duduwolf.handgis.service.TreeHandler;
import com.duduwolf.handgis.service.XMLTreeHandler;

/**
 * Servlet implementation class TreeLoader
 */
public class TreeLoader extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TreeLoader() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String result = treeHandler.treeHandle();
		log.debug(result);
		if (result != null) {
			try {
	            response.setContentType("text/xml;charset=UTF-8");
	            response.getWriter().write(result);
	        } catch (IOException e) {
	            log.error("向浏览器输出XML文件出错：{}", e.getMessage());
	            e.printStackTrace();
	        }
		}
	}
	
	private TreeHandler treeHandler = new XMLTreeHandler();

	static final Logger log = LoggerFactory.getLogger(TreeLoader.class);
}
